package tgc.edu.exam.web.controller;

import java.util.HashSet;
import java.util.List;
import java.util.UUID;

import javax.persistence.criteria.CriteriaBuilder;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Predicate;
import javax.persistence.criteria.Root;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import tgc.edu.exam.custom.CommonController;
import tgc.edu.exam.custom.DataGridParam;
import tgc.edu.exam.custom.DataGridUtils;
import tgc.edu.exam.custom.LayUiResult;
import tgc.edu.exam.entity.ActivityClass;
import tgc.edu.exam.entity.TbClass;
import tgc.edu.exam.service.ActivityClassService;
import tgc.edu.exam.service.TbClassService;
import tgc.edu.exam.web.form.ActivityClassForm;

@Controller
@RequestMapping(value="/activityClass")
public class ActivityClassController extends CommonController<ActivityClass, UUID, ActivityClassForm>{
	@Autowired
	private ActivityClassService activityClassDAO;
	@Autowired
	private TbClassService csService;
	@Override
	public void edit(ActivityClassForm form, ModelMap map) throws InstantiationException, IllegalAccessException {
		List<TbClass> bjs = csService.findAll();
		map.put("bjs", bjs);
		super.edit(form, map);
	}
	
	@Override
	public Specification<ActivityClass> buildSpec(ActivityClassForm form) {
		Specification<ActivityClass> specification = new Specification<ActivityClass>() {

			private static final long serialVersionUID = 1L;
			
			@Override
			public Predicate toPredicate(Root<ActivityClass> root, CriteriaQuery<?> query, CriteriaBuilder cb) {
				HashSet<Predicate> rules=new HashSet<>();
				if(StringUtils.hasText(form.getMotif())) {
					Predicate name = cb.like(root.get("motif"), "%"+form.getMotif()+"%");
					rules.add(name);
				}
				if(StringUtils.hasText(form.getContent())) {
					Predicate difficultylevel = cb.like(root.get("content"), "%"+form.getContent()+"%");
					rules.add(difficultylevel);
				}
				return cb.and(rules.toArray(new Predicate[rules.size()]));
			}

		};
		return specification;
	}
}
